package com.tuling.jucdemo.sync;

import java.time.LocalDateTime;

/**
 * @author :tangweiliang
 * @date : 2022/11/12 16:51
 * 未来的你会感谢现在努力的自己
 *
 * 锁消除  锁粗化
 */
public class LockEliminationTest {

    StringBuffer buffer = new StringBuffer();
    //锁粗化
    public void append(){
        buffer.append("aaa").append("bbb").append("ccc");
    }

    /**
     * 锁消除
     *  ‐XX:+EliminateLocks 开启锁消除(jdk8默认开启） 时间2048ms
     *  ‐XX:‐EliminateLocks 关闭锁消除 时间5822ms
     * @param str1
     * @param str2
     */
    public void append(String str1,String str2){
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(str1).append(str2);
    }

    public static void main(String[] args) {
        LockEliminationTest demo = new LockEliminationTest();
        long start = System.currentTimeMillis();
        for (int i = 0; i < 100000000; i++) {
            demo.append("aaa","bbb");
        }
        long end = System.currentTimeMillis();
        System.out.println("执行时间:"+(end - start)+"ms");
    }
}
